//
//  StatusCell.swift
//  sinaWeibo
//
//  Created by LSY on 15/12/22.
//  Copyright © 2015年 lsy. All rights reserved.
//

import UIKit
import SnapKit
class StatusCell: UITableViewCell {
    
    var bottomViewTopConstraints :Constraint?
    //    设置status模型
    var status :Status?{
        
        didSet{
            
            topView.status = status
            
//            删除bottomView的约束
            bottomViewTopConstraints?.uninstall()
//           进行判断
            if let retweetedStatus = status?.retweeted_status{
//            走到这肯定是转发微博，设置约束
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.bottomViewTopConstraints  = make.top.equalTo(retweetedView.snp_bottom).constraint
                })
//                设置数据源
                retweetedView.retweetedStatus = status?.retweeted_status
//                显示转发微博
                retweetedView.hidden = false
                
            }else{
//            不是转发微博，设置约束用topView的底部
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomViewTopConstraints = make.top.equalTo(topView.snp_bottom).constraint

                })
                
//            此时会出现bug,topView遮盖住bottomView
//            隐藏转发微博
                retweetedView.hidden = true
            
            }
        }
    }
    //    初始化的方法cell的入口
    override init(style:UITableViewCellStyle,reuseIdentifier:String?){
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //        跟新UI
        setupUI()
        
//       设置cell的选中样式
        selectionStyle = .None
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private  func setupUI(){
        //    添加顶部视图
        contentView.addSubview(topView)
        //    添加转发微博的视图
        contentView.addSubview(retweetedView)
        //    添加底部视图
        contentView.addSubview(bottomView)
        
        //    topView设置约束
        topView.snp_makeConstraints { (make) -> Void in
        //    设置topView的宽高等
            make.top.left.right.equalTo(contentView)
        }
        //   设置转发微博的约束
        retweetedView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(topView.snp_bottom)
            make.left.right.equalTo(contentView)
            
//            make.height.equalTo(50)
            
        }
        //    bottomView设置约束
        bottomView.snp_makeConstraints { (make) -> Void in
            
            make.right.left.equalTo(self)
//           获取bottomView的约束
          self.bottomViewTopConstraints = make.top.equalTo(retweetedView.snp_bottom).constraint
            make.height.equalTo(40)
        }
        
        //    给contentView设置约束，这样可以使cell自动计算行高
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.top.left.right.equalTo(self)
            //        设置bottom的约束参考的是bottomView的底部，这样可以进行自动计算cell的行高
            make.bottom.equalTo(bottomView.snp_bottom)
        }
    }
    //   懒加载自定义的cell
    private  lazy var topView : StatusCellTopView = StatusCellTopView()
    //    转发微博的视图
    private lazy var retweetedView:StatusCellRetweetedView = StatusCellRetweetedView()
    //     底部视图
    private lazy var bottomView :StatusCellBottomView = StatusCellBottomView()
    
}
